{{extend 'layout.html'}}
<script type="text/javascript" src="/{{=request.application}}/static/js/jquery.timeago.js"></script>
<script src="{{=URL('static','js/tagging_ajax.js')}}"></script>
<script src="{{=URL('static','js/grouping_ajax.js')}}"></script>
{{=json}}
<script>
var msg_id = {{=id}};

$(function(){
    
    $('.timestamp').timeago()

    click_once_tag();            
    click_once_group();            
})

{{if auth.has_membership('Admin'):}}    
    $('.delete').live('click', function(){
        var answer = confirm('Are you sure you want to delete this?')
        if (answer){
            return true
        }     
        return false
        
    })
{{pass}}    



function addgroups(widget){    
    groups = $.grep(groups, function(group){ return group.role != widget.target.name } )
    
    $.ajax({
        url: '/msghandler/default/insert_ajax',
        data: 'group=' + this.id  + '&id=' + msg_id + '&table=msg_group'
        })             
    
    td1 = $('<img>').attr({
            src:'/msghandler/static/images/delete.png',
            class:'groups-add',
            hidden:true,
            name:widget.target.name,
            id: 'img' + widget.target.id,
        });

    $(widget.target).parent().fadeOut(function() { $(this).remove(); });
    $('#tr-groups-new table tr').append($('<td class="top-td">').append($('<span>'+ widget.target.name +'</span>'),td1));
    
    $('.groups-add').unbind();
    click_once_group();
}

function click_once_group(){
    $('.groups-add').one('click', function(){
        $(this).parent().fadeOut( function() { $(this).remove();});
        id = parseInt(this.id.split('imgt')[1]);
        tag = {id:id, role:this.name};
        groups.push(tag); 
        $.ajax({
            url: '/msghandler/default/delete_ajax',
            data: 'group=' + id  + '&id=' + msg_id + '&table=msg_group'
        })             
        showgroups();                 
    });
}
</script>
<style>
.td_img {text-align:center;}
</style>
<style>
tr.content,tr.attachment { 
    border: 1px solid black;
    width: 95%;
}
</style>

REFERRAL
<table width='95%'>
<tr><td><h4>{{=message.subject}}</h4></td><tr/>
<tr>
    <td>From:{{=message.created_by.user_id.first_name + ' ' + message.created_by.user_id.last_name}} ({{=SPAN(_title=message.create_time,_class='timestamp')}}) | 
    	{{if not message.is_hidden:}}
    		{{=A('Remove referral', _class='delete', _href=URL('delete', vars=dict(id=id, msg_id=0)))}}
	{{pass}}
	</td>
</tr>
{{if update_time:}}
<tr><td>Updated {{=SPAN(_title=update_time,_class='timestamp')}}</td></tr>
{{pass}}
<tr class='content'><td>{{=message.content}}</td></tr>
<tr><td><br/></td></td>
<tr>
<table>
<tr><td width='15%'>Groups:</td>
    <td>{{=INPUT(_placeholder='search groups',_name='groups', _onkeyup='showgroups()', _autocomplete='off' )}}</td>
    <td id='tr-groups-new'><table><tr>
{{if len(groups):}}    
    {{for group in groups:}}
        <td class='top-td'>
            <span>{{=group.group_id.role}}</span>
            {{=IMG(_src=URL('static', 'images/delete.png'), _hidden=True, _class='groups-add', _id='imgt'+ `group.group_id.id`, _name=group.group_id.role)}}
        </td>
    {{pass}}
{{pass}}    
    </tr></table></td>
</tr>    
<tr><td></td><td><div id='new-groups'></div></td><tr>

<tr><td>Tags:</td>
    <td>{{=INPUT(_placeholder='search tags', _name='tags', _onkeyup='showtags()', _autocomplete='off')}}</td>
    <td id='tr-tags-new'><table><tr>
{{if len(tags):}}    
    {{for tag in tags:}}
        <td class='top-td'>
            <span>{{=tag.tag_id.name}}</span>
            {{=IMG(_src=URL('static', 'images/delete.png'), _hidden=True, _class='tags-add', _id='imgt'+ `tag.tag_id.id`, _name=tag.tag_id.name)}}
        </td>
    {{pass}}
{{pass}}    
    </tr></table></td>
</tr>
<tr><td></td><td><div id='new-tags'></div></td><tr>
</tr>
</table>
<br/>
</table>
ATTACHMENTS | {{=A('Upload new attachments', _href=URL('attachments','create',args=id))}}<br />
{{if len(attachs) > 0:}}
<table id='attachments' width=95%>
{{for attach in attachs:}}
{{user = attach['attachment'].attach_by.user_id.first_name + ' ' + attach['attachment'].attach_by.user_id.last_name}}
<tr class='attachment'>
{{=TD(A(IMG(_src=attach['src'], _alt="attachments", _height='50px'),
      _href=URL('default','download',args=attach['attachment'].attachment)), _class='td_img')}}
{{if auth.has_membership('Admin') or auth.has_membership('Telehealth') or contacts.find(lambda row: row.id==attach['attachment'].attach_by):}}
{{=TD(attach['attachment'].filename,' | ',A('Remove this attachment', _class='delete', _href=URL('attachments','delete', vars=dict(id=attach['attachment'].id,msg_id=id))),BR(),'added by ', SPAN(user,' ', SPAN(_title=attach['attachment'].attach_time,_class='timestamp') ))}}
{{else:}}
{{=TD(attach['attachment'].filename,BR(),'added by ', SPAN(user,' ', SPAN(_title=attach['attachment'].attach_time,_class='timestamp') ))}}

{{pass}}
</tr>
{{pass}}
</table>
{{pass}}
<br/>
REPLIES
<table width=95%>
{{for reply in replies:}}
{{user = reply.created_by.user_id.first_name + ' ' + reply.created_by.user_id.last_name}}
    {{if reply.is_hidden:}}
<tr><td><del>From {{=user}} {{=SPAN(_title=reply.create_time,_class='timestamp')}}</del></td></tr>
<tr class='content'><td colspan=2><del>{{=reply.content}}</del></td></tr>
    {{else:}}
        {{if auth.has_membership('Admin') or auth.has_membership('Telehealth') or contacts.find(lambda row: row.id==reply.created_by):}}
<tr><td>From {{=user}} {{=SPAN(_title=reply.create_time,_class='timestamp')}} | {{=A('Remove reply', _class='delete', _href=URL('delete', vars=dict(id=reply.id, msg_id=id)))}}</td></tr>
<tr class='content'><td colspan=2>{{=reply.content}}</td></tr>
        {{else:}}
<tr><td>From {{=user}} {{=SPAN(_title=reply.create_time,_class='timestamp')}}</td></tr>
<tr class='content'><td colspan=2>{{=reply.content}}</td></tr>        
        {{pass}}
    {{pass}}
{{pass}}
{{=form}}
</table>
